package busterminalweb.action.user;

import busterminalweb.action.Action;
import busterminalweb.action.ActionResult;
import busterminalweb.authorization.Authorization;
import busterminalweb.authorization.User;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UserLogin implements Action {

    private static String USER_AUTH = "userAuth";

    @Override
    public ActionResult execute(HttpServletRequest request, HttpServletResponse response) {

        String login = request.getParameter("login");
        String password = request.getParameter("password");
        if (login != null && password != null) {
            Authorization authorization = new Authorization();
            if (authorization.isValidLogin(login, password)) {
                User user = authorization.getUser(login, password);                               
                request.getSession().setAttribute(USER_AUTH, user);
            }
       }

        //main - это action, который никак сервлетом не обрабатывается,
        //т.е. открывается главная страница
        return ActionResult.redirect("main");
    }

    @Override
    public String getName() {
        return "user.login";
    }
}
